#include "setviewdialog.h"

SetViewDialog::SetViewDialog( QString table,
                              QString num,
                              QString name,
                              QMap<int, int> asc_indices_arg,
                              QMap<int, int> desc_indices_arg )
{
album = table;

asc_indices = asc_indices_arg;
desc_indices = desc_indices_arg;
setupUi(this);

QString s = QLatin1String("<b>");
s.append(num);
s.append( QLatin1String(" ") );
s.append(name);
label->setText(s);

set_model();
QPushButton * okButton = qobject_cast<QPushButton *>( buttonBox->button( QDialogButtonBox::Ok ) );
QObject::connect( okButton,
                  SIGNAL( clicked() ),
                  this,
                  SLOT( commit_table() ) );
}

SetViewDialog::~SetViewDialog()
{
}

void SetViewDialog::set_model()
{
QSqlDatabase db = QSqlDatabase::database("collection", false);
    if ( !db.isOpen() )
    {
    QMessageBox::warning( this,
                          tr("Error"),
                          tr("Database is closed.") );
    return;
    }

    if ( true == album.isEmpty() )
    {
    return;
    }

QString options_table = album;
options_table.append( QLatin1String("_options") );
TabModel3 * model = new TabModel3( 0, db, asc_indices, desc_indices );
model->setEditStrategy( QSqlTableModel::OnManualSubmit );
model->setTable(options_table);
model->setSort(0, Qt::AscendingOrder);
model->select();
QString header0 = tr("Column");
QString header1 = tr("Asc. Index");
QString header2 = tr("Desc. Index");
QString header3 = tr("Show/Hide");
QString header4 = tr("Width (px)");
model->setHeaderData(0, Qt::Horizontal, header0);
model->setHeaderData(1, Qt::Horizontal, header1);
model->setHeaderData(2, Qt::Horizontal, header2);
model->setHeaderData(3, Qt::Horizontal, header3);
model->setHeaderData(4, Qt::Horizontal, header4);
tableView->setModel(model);
tableView->setItemDelegateForColumn( 3, new ComBoxDelegate2() );
tableView->setItemDelegateForColumn( 4, new SpinBoxDelegate() );
tableView->hideColumn(5);
tableView->hideColumn(6);
}

void SetViewDialog::delete_model()
{
TabModel3 * model = qobject_cast<TabModel3 *>( tableView->model() );
    if (NULL != model)
    {
    model->clear();
    }
tableView->setModel( new QSqlTableModel() );
delete model;
model = NULL;
}

void SetViewDialog::closeEvent(QCloseEvent * e)
{
delete_model();
e->accept();
}

void SetViewDialog::commit_table()
{
QSqlDatabase db = QSqlDatabase::database("collection", false);
    if ( !db.isOpen() )
    {
    QMessageBox::warning( this,
                          tr("Error"),
                          tr("Database is closed.") );
    return;
    }

    if ( true == album.isEmpty() )
    {
    return;
    }

TabModel3 * model = qobject_cast<TabModel3 *>( tableView->model() );
    if (NULL != model)
    {
    bool model_submit = model->submitAll();
        if (!model_submit)
        {
        QMessageBox::critical( this,
                               tr("Model submit failed"),
                               tr("The database reported : %1").arg(model->lastError().text() ) );
        model->revertAll();
        return;
        }
    }
}

int SetViewDialog::return_selection()
{
    if ( true == checkBox->isChecked() )
    {
    return 1;
    }
return 0;
}
